﻿/*
 * Source code compressor
 * 
 * initial implementation : 15.10.2011
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace SourPresser
{



  class Program
  {

    private static string _directory;
    private static DataProcessDirectionEnum _direction;
    private static string _keyFileName;

    static void Main(string[] args)
    {

      //Console.WriteLine(args.Length);

      //foreach (string arg in args)
      //  Console.WriteLine(arg);

      if (args.Length != 3)
      {
        Console.WriteLine("arguments missing (direction, path, key)");
        return;
      }

      _direction = _getDirection(args[0]);
      if (_direction == DataProcessDirectionEnum.undefined)
      {
        Console.WriteLine("direction arguments invalid (-d or -c required)");
        return;
      } 

      if (!System.IO.Directory.Exists(args[1]))
      {
        Console.WriteLine("invalid input directory");
        return;
      }
      else
        _directory = args[1];

      _keyFileName = args[2];

      processFiles("*.vb", SourceFileTypeEnum.vbFile);
      processFiles("*.cs", SourceFileTypeEnum.csFile);
      processFiles("*.xml", SourceFileTypeEnum.xmlFile);
      processFiles("*.xaml", SourceFileTypeEnum.xmlFile);

    }




    private static void processFiles(string fileMask, SourceFileTypeEnum type)
    {
      //begin vb resolve
      string[] files = Directory.GetFiles(_directory,
      fileMask,
      SearchOption.AllDirectories);

      // Process files here.
      foreach (string file in files)
      {
        if (_direction == DataProcessDirectionEnum.dataCompress)
          Processor.CompressFile(file, type, _keyFileName);
        else
          Processor.DecompressFile(file, type, _keyFileName);

        Console.WriteLine("file {0} processed", file);
      }
    }





    private static DataProcessDirectionEnum _getDirection(string arg)
    {
      if (arg == "-d")
          return DataProcessDirectionEnum.dataDecompress;

      if (arg == "-c")
        return DataProcessDirectionEnum.dataCompress;

      return DataProcessDirectionEnum.undefined;
    }



  }
}
